package view;
import java.awt.Dimension;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;


/**
 * The Class AddPhotoToDBGUI.
 * @Tomer
 */
public class AddPhotoToDBGUI extends AbstractGUI{
	
	/** The lbl location. */
	private JLabel lblLocation = null;
	
	/** The lbl time. */
	private JLabel lblTime = null;
	
	/** The btn search. */
	private JButton btnSearch = null;
	
	/** The txt field location. */
	private JTextField txtFieldLocation = null;
	
	/** The day spinner. */
	private JSpinner daySpinner;
	
	/** The month spinner. */
	private JSpinner monthSpinner;
	
	/** The year spinner. */
	private JSpinner yearSpinner;
	
	/** The lbl year. */
	private JLabel lblYear;
	
	/** The separator1. */
	private JSeparator separator1;
	
	/** The separator2. */
	private JSeparator separator2;
	
	/** The separator3. */
	private JSeparator separator3;
	
	/** The lbl coordinates. */
	private JLabel lblCoordinates;
	
	/** The hour spinner. */
	private JSpinner hourSpinner;
	
	/** The minuet spinner. */
	private JSpinner minuetSpinner;
	
	/** The sec spinner. */
	private JSpinner secSpinner;
	
	/** The dots1. */
	private JLabel dots1;
	
	/** The dots2. */
	private JLabel dots2;
	
	/** The lbl hour. */
	private JLabel lblHour;
	
	/** The lbl minuet. */
	private JLabel lblMinuet;
	
	/** The lbl sec. */
	private JLabel lblSec;
	
	/** The slash1. */
	private JLabel slash1;
	
	/** The slash2. */
	private JLabel slash2;
	
	
	//spinner models
	/** The year strings. */
	private String[] yearStrings = {"2011","2012","2013","2014"};
	
	/** The month strings. */
	private String[] monthStrings;
	
	/** The day strings. */
	private String[] dayStrings;
	
	/** The hour strings. */
	private String[] hourStrings;
	
	/** The min strings. */
	private String[] minStrings;
	
	/** The sec strings. */
	private String[] secStrings;
	
	/** The year model. */
	private SpinnerListModel yearModel;
	
	/** The month model. */
	private SpinnerListModel monthModel;
	
	/** The day model. */
	private SpinnerListModel dayModel;
	
	/** The hour model. */
	private SpinnerListModel hourModel;
	
	/** The min model. */
	private SpinnerListModel minModel;
	
	/** The sec model. */
	private SpinnerListModel secModel;
	
	/** The window name. */
	private String windowName;
	
	/** The text field. */
	private JTextField textField;
	

	/**
	 * Instantiates a new adds the photo to dbgui.
	 *
	 * @param windowName the window name
	 */
	public AddPhotoToDBGUI(String windowName) 
	{
		this.windowName = windowName;
		getContentPane().setLayout(null);
		
		//fill the spinners with content
		
		yearModel = new SpinnerListModel(yearStrings);
		getContentPane().add(getYearSpinner());
		
		monthStrings = createTimeStrings(12);
		monthModel = new SpinnerListModel(monthStrings);
		getContentPane().add(getMonthSpinner());
		
		dayStrings = createTimeStrings(31);
		dayModel = new SpinnerListModel(dayStrings);
		getContentPane().add(getDaySpinner());
		
		hourStrings = createTimeStrings(24);
		hourStrings[23] = "00";
		hourModel = new SpinnerListModel(hourStrings);
		getContentPane().add(getHourSpinner());
		
		minStrings = createTimeStrings(60);
		minStrings[59] = "00";
		minModel = new SpinnerListModel(minStrings);
		getContentPane().add(getMinuetSpinner());
		
		secStrings = createTimeStrings(60);
		secStrings[59] = "00";
		secModel = new SpinnerListModel(secStrings);
		getContentPane().add(getSecSpinner());
		
		JLabel lblDay = new JLabel("day");
		lblDay.setBounds(20, 41, 42, 20);
		getContentPane().add(lblDay);
		
		JLabel lblMonth = new JLabel("month");
		lblMonth.setBounds(71, 41, 54, 20);
		
		getContentPane().add(getLblLocation());
		getContentPane().add(getLblTime());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getTxtFieldLocation());
		getContentPane().add(getBackButton());		
		getContentPane().add(lblMonth);
		getContentPane().add(getLblYear());
		getContentPane().add(getSeparator1());
		getContentPane().add(getSeparator2());
		getContentPane().add(getLblCoordinates());
		getContentPane().add(getDots1());
		getContentPane().add(getDots2());
		getContentPane().add(getLblHour());
		getContentPane().add(getLblMinuet());
		getContentPane().add(getLblSec());
		getContentPane().add(getSlash2());
		getContentPane().add(getSlash1());
		getContentPane().add(getSeparator3());
		
		JLabel lblLinkToFile = new JLabel("Link To New File");
		lblLinkToFile.setBounds(10, 190, 138, 20);
		getContentPane().add(lblLinkToFile);
		
		textField = new JTextField();
		textField.setBounds(10, 232, 146, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		this.setTitle(windowName);
		this.setBounds(500, 200, 440, 388);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
	}

	/**
	 * Gets the txt field location.
	 *
	 * @return the txt field location
	 */
	private JTextField getTxtFieldLocation() {
		if(txtFieldLocation == null){
			txtFieldLocation = new JTextField();
			txtFieldLocation.setBounds(10, 154, 138, 20);
			txtFieldLocation.setColumns(10);
		}
		return txtFieldLocation;
	}
	
	/**
	 * Creates the time strings.
	 *
	 * @param limit the limit
	 * @return the string[]
	 */
	private String[] createTimeStrings(int limit)
	{
		String [] res = new String[limit];
		for (int i = 0; i < limit; i++) 
		{
			res[i] = (i<9 ? "0":"") + (i+1);
		}
		
		return res;
	}

	/**
	 * Gets the lbl time.
	 *
	 * @return the lbl time
	 */
	private JLabel getLblTime() {
		if(lblTime == null){
			lblTime = new JLabel("Time");
			lblTime.setBounds(10, 11, 46, 14);
		}
		return lblTime;
	}

	/**
	 * Gets the lbl location.
	 *
	 * @return the lbl location
	 */
	private JLabel getLblLocation() {
		if(lblLocation == null){
			lblLocation = new JLabel("Location");
			lblLocation.setBounds(10, 102, 69, 14);
		}
		return lblLocation;
	}
	
	/**
	 * Gets the btn search.
	 *
	 * @return the btn search
	 */
	private JButton getBtnSearch() {
		if(btnSearch == null) {
			btnSearch = new JButton(windowName);
			btnSearch.setBounds(18, 291, 153, 26);
		}
		return btnSearch;
	}
	
	/**
	 * Gets the back button.
	 *
	 * @return the back button
	 */
	private JButton getBackButton() 
	{
		if (backButton == null) {
			backButton = new JButton("Back");
			backButton.setLocation(284, 0);
			backButton.setLocation(284, 0);
			backButton.setMinimumSize(new Dimension(79, 29));
			backButton.setMaximumSize(new Dimension(79, 29));
			backButton.setBounds(276, 291, 115, 27);
		}
		return backButton;
	}
	
	/**
	 * Gets the day spinner.
	 *
	 * @return the day spinner
	 */
	private JSpinner getDaySpinner() {
		if (daySpinner == null) {
			daySpinner = new JSpinner(dayModel);
			daySpinner.setBounds(10, 60, 46, 26);
		}
		return daySpinner;
	}
	
	/**
	 * Gets the month spinner.
	 *
	 * @return the month spinner
	 */
	private JSpinner getMonthSpinner() 
	{
		if (monthSpinner == null) 
		{
			monthSpinner = new JSpinner(monthModel);
			monthSpinner.setBounds(71, 60, 46, 26);
		}
		return monthSpinner;
	}
	
	/**
	 * Gets the year spinner.
	 *
	 * @return the year spinner
	 */
	private JSpinner getYearSpinner() {
		if (yearSpinner == null) {
			yearSpinner = new JSpinner(yearModel);
			yearSpinner.setBounds(128, 60, 69, 26);
		}
		return yearSpinner;
	}
	
	/**
	 * Gets the lbl year.
	 *
	 * @return the lbl year
	 */
	private JLabel getLblYear() {
		if (lblYear == null) {
			lblYear = new JLabel("year");
			lblYear.setBounds(141, 41, 30, 20);
		}
		return lblYear;
	}
	
	/**
	 * Gets the separator1.
	 *
	 * @return the separator1
	 */
	private JSeparator getSeparator1() {
		if (separator1 == null) {
			separator1 = new JSeparator();
			separator1.setBounds(10, 30, 381, 14);
		}
		return separator1;
	}
	
	/**
	 * Gets the separator2.
	 *
	 * @return the separator2
	 */
	private JSeparator getSeparator2() {
		if (separator2 == null) {
			separator2 = new JSeparator();
			separator2.setBounds(10, 118, 375, 2);
		}
		return separator2;
	}
	
	/**
	 * Gets the lbl coordinates.
	 *
	 * @return the lbl coordinates
	 */
	private JLabel getLblCoordinates() {
		if (lblCoordinates == null) {
			lblCoordinates = new JLabel("Cordinates");
			lblCoordinates.setBounds(10, 132, 107, 20);
		}
		return lblCoordinates;
	}
	
	/**
	 * Gets the hour spinner.
	 *
	 * @return the hour spinner
	 */
	private JSpinner getHourSpinner() {
		if (hourSpinner == null) {
			hourSpinner = new JSpinner(hourModel);
			hourSpinner.setBounds(223, 60, 46, 26);
		}
		return hourSpinner;
	}
	
	/**
	 * Gets the minuet spinner.
	 *
	 * @return the minuet spinner
	 */
	private JSpinner getMinuetSpinner() {
		if (minuetSpinner == null) {
			minuetSpinner = new JSpinner(minModel);
			minuetSpinner.setBounds(284, 60, 46, 26);
		}
		return minuetSpinner;
	}
	
	/**
	 * Gets the sec spinner.
	 *
	 * @return the sec spinner
	 */
	private JSpinner getSecSpinner() {
		if (secSpinner == null) {
			secSpinner = new JSpinner(secModel);
			secSpinner.setBounds(345, 60, 46, 26);
		}
		return secSpinner;
	}
	
	/**
	 * Gets the dots1.
	 *
	 * @return the dots1
	 */
	private JLabel getDots1() {
		if (dots1 == null) {
			dots1 = new JLabel(":");
			dots1.setBounds(275, 63, 6, 20);
		}
		return dots1;
	}
	
	/**
	 * Gets the dots2.
	 *
	 * @return the dots2
	 */
	private JLabel getDots2() {
		if (dots2 == null) {
			dots2 = new JLabel(":");
			dots2.setBounds(336, 63, 6, 20);
		}
		return dots2;
	}
	
	/**
	 * Gets the lbl hour.
	 *
	 * @return the lbl hour
	 */
	private JLabel getLblHour() {
		if (lblHour == null) {
			lblHour = new JLabel("hour");
			lblHour.setBounds(227, 41, 42, 20);
		}
		return lblHour;
	}
	
	/**
	 * Gets the lbl minuet.
	 *
	 * @return the lbl minuet
	 */
	private JLabel getLblMinuet() {
		if (lblMinuet == null) {
			lblMinuet = new JLabel("minuet");
			lblMinuet.setBounds(284, 41, 69, 20);
		}
		return lblMinuet;
	}
	
	/**
	 * Gets the lbl sec.
	 *
	 * @return the lbl sec
	 */
	private JLabel getLblSec() {
		if (lblSec == null) {
			lblSec = new JLabel("sec");
			lblSec.setBounds(361, 41, 30, 20);
		}
		return lblSec;
	}
	
	/**
	 * Gets the slash2.
	 *
	 * @return the slash2
	 */
	private JLabel getSlash2() {
		if (slash2 == null) {
			slash2 = new JLabel("/");
			slash2.setBounds(119, 63, 6, 20);
		}
		return slash2;
	}
	
	/**
	 * Gets the slash1.
	 *
	 * @return the slash1
	 */
	private JLabel getSlash1() {
		if (slash1 == null) {
			slash1 = new JLabel("/");
			slash1.setBounds(60, 63, 6, 20);
		}
		return slash1;
	}
	
	/**
	 * Gets the separator3.
	 *
	 * @return the separator3
	 */
	private JSeparator getSeparator3() {
		if (separator3 == null) {
			separator3 = new JSeparator();
			separator3.setBounds(10, 214, 375, 2);
		}
		return separator3;
	}
	
	/**
	 * Public Methods.
	 /
	 
	 
	 
	 /**
	 *
	 *	Get the text field content.
	 *
	 * @return the link to file
	 */
	
	public String getLinkToFile()
	{
		return textField.getText();
	}
	
	/***
	 * This method returns the location typed in the location text field.
	 * @return location
	 */
	public String getLocationString()
	{
		return	txtFieldLocation.getText();
	}
	
	/**
	 * *
	 * This method sets the location text field. 
	 *
	 * @param location the new location string
	 */
	public void setLocationString(String location)
	{
		txtFieldLocation.setText(location);
	}
	
	/**
	 * This method returns the selected timeStamp in "yyyy-MM-dd HH:mm:ss" format.
	 *
	 * @return timeStamp string
	 */
	public String getTimestamp()
	{
		String yyyy = (String) getYearSpinner().getValue();
		String MM = (String) getMonthSpinner().getValue();
		String dd = (String) getDaySpinner().getValue();
		String HH = (String) getHourSpinner().getValue();
		String mm = (String) getMinuetSpinner().getValue();
		String ss = (String) getSecSpinner().getValue();
		
		// set the timestamp format: "yyyy-MM-dd HH:mm:ss"
		String timeStamp = yyyy+"-"+MM+"-"+dd+" "+HH+":"+mm+":"+ss;
		return timeStamp;
	}
	
	/**
	 * This method adds an action listener to search button.
	 *
	 * @param listener the listener
	 */
	public void addSearchActionListener(ActionListener listener){
		btnSearch.addActionListener(listener);
	}
}
